/*
 +-----------------------------------------------------------------------+
 | RoundCube Webmail Client Script										 |
 |																		 |
 | This file is part of the RoundCube Webmail client					 |
 | Copyright (C) 2008-2009, RoundCube Dev, - Switzerland 	             |
 | Licensed under the GNU GPL											 |
 |																		 |
 +-----------------------------------------------------------------------+
 | Author: Philip Weir													 |
 +-----------------------------------------------------------------------+

 $Id: $

*/

function select_all_langs() {
	var langlist = document.getElementsByName('_spamlang[]');
	var obj;

	for (var i = 0; i < langlist.length; i++) {
		langlist[i].checked = true;
		obj = rcube_find_object('spam_lang_'+ i)
		obj.title = rcmail.get_label('enabled');
		obj.innerHTML = rcube_find_object('enable_button').innerHTML;
	}

	return false;
}

function select_no_langs() {
	var langlist = document.getElementsByName('_spamlang[]');
	var obj;

	for (var i = 0; i < langlist.length; i++) {
		langlist[i].checked = false;
		obj = rcube_find_object('spam_lang_'+ i)
		obj.title = rcmail.get_label('disabled');
		obj.innerHTML = rcube_find_object('disable_button').innerHTML;
	}

	return false;
}

function message_lang(lang_code, obj) {
	var langlist = document.getElementsByName('_spamlang[]');
	var i = obj.parentNode.parentNode.rowIndex - 2;

	if (langlist[i].checked) {
		langlist[i].checked = false;
		obj.title = rcmail.get_label('disabled');
		obj.innerHTML = rcube_find_object('disable_button').innerHTML;
	}
	else {
		langlist[i].checked = true;
		obj.title = rcmail.get_label('enabled');
		obj.innerHTML = rcube_find_object('enable_button').innerHTML;
	}

	return false;
}

function addressrule_del(obj) {
	var adrTable = rcube_find_object('address-rules-table').tBodies[0];
	var rowidx = obj.parentNode.parentNode.rowIndex - 1;
	var fieldidx = rowidx - 1;

	if (!confirm(rcmail.get_label('spamaddressdelete')))
		return false;

	if (document.getElementsByName('_address_rule_act[]')[fieldidx].value == "INSERT") {
		adrTable.deleteRow(rowidx);
	}
	else {
		adrTable.rows[rowidx].style.display = 'none';
		document.getElementsByName('_address_rule_act[]')[fieldidx].value = "DELETE";
	}

	rcmail.env.address_rule_count--;
	if (rcmail.env.address_rule_count < 1)
		adrTable.rows[1].style.display = '';

	return false;
}

function addressrule_add() {
	var adrTable = rcube_find_object('address-rules-table').tBodies[0];
	var input_spamaddressrule = rcube_find_object('_spamaddressrule');
	var selrule = input_spamaddressrule.selectedIndex;
	var input_spamaddress = rcube_find_object('_spamaddress');

	if (input_spamaddress.value.replace(/^\s+|\s+$/g, '') == '') {
		alert(rcmail.get_label('spamenteraddress'));
		input_spamaddress.focus();
		return false;
	}
	else if (!rcube_check_email(input_spamaddress.value.replace(/^\s+/, '').replace(/[\s,;]+$/, ''), true)) {
		alert(rcmail.get_label('spamaddresserror'));
		input_spamaddress.focus();
		return false;
	}
	else {
		var actions = document.getElementsByName('_address_rule_act[]');
		var prefs = document.getElementsByName('_address_rule_field[]');
		var addresses = document.getElementsByName('_address_rule_value[]');
		var insHere;

		for (var i = 1; i < addresses.length; i++){
			if (addresses[i].value == input_spamaddress.value && actions[i].value != "DELETE") {
				alert(rcmail.get_label('spamaddressexists'));
				input_spamaddress.focus();
				return false;
			}
			else if (addresses[i].value > input_spamaddress.value) {
				insHere = adrTable.rows[i + 1];
				break;
			}
		}

		var newNode = adrTable.rows[0].cloneNode(true);
		adrTable.rows[1].style.display = 'none';

		if (insHere)
			adrTable.insertBefore(newNode, insHere);
		else
			adrTable.appendChild(newNode);

		newNode.style.display = "";
		newNode.cells[0].className = input_spamaddressrule.options[selrule].value;
		newNode.cells[0].innerHTML = input_spamaddressrule.options[selrule].text;
		newNode.cells[1].innerHTML = input_spamaddress.value;
		actions[newNode.rowIndex - 2].value = "INSERT";
		prefs[newNode.rowIndex - 2].value = input_spamaddressrule.options[selrule].value;
		addresses[newNode.rowIndex - 2].value = input_spamaddress.value;

		input_spamaddressrule.selectedIndex = 0;
		input_spamaddress.value = '';

		rcmail.env.address_rule_count++;
	}
}

function toggle_level_char(checkbox) {
	var level_char;
	if (level_char = rcube_find_object('rcmfd_spamlevelchar'))
		level_char.disabled = !checkbox.checked;
}